import tank
import shotgun_api3
import pymel.core as pm

def get_asset_instance(types=[]):
    """
    # 1. 获取资产的shaderNetwork和所有abc的ref节点
    # 2. 获取资产对应shaderNetwork的shaderMapping数据
    return:
    {u'shaderMappingTest': {'id': 2201,
                            'items': [u'shaderMappingTest_Model_v001RN',
                                      u'shaderMappingTest_Model_v001RN1'],
                            'shader_network': u'shaderMappingTest_Shading_v001RN',
                            'shader_mapping': path},
    }
    """
    asset_dict = {}
    ref_object = scan_scene_ref()
    all_data = get_asset_from_ref(ref_object, types=types)
    for data in all_data:
        asset_name = data["fields"]["Asset"]
        if asset_name not in asset_dict:
            asset_id = data["sg_data"]["entity"]["id"]
            asset_dict[asset_name] = {'id':asset_id}
            asset_dict[asset_name]['items'] = []
            asset_dict[asset_name]["shader_network"] = []
            asset_dict[asset_name]["shader_mapping"] = []
        asset_ref_node = data["node_name"]
        asset_publish = data["sg_data"]["published_file_type"]["name"]
        if asset_publish == "Shader Network":
            # 如果有多个shader文件，只使用第一个 （To Do ）
            if asset_dict[asset_name]["shader_network"]:
                continue
            asset_dict[asset_name]["shader_network"] = asset_ref_node

            # 获取最高版本的关系文件
            asset_shader_mapping_data = get_asset_published_files(asset_id=asset_dict[asset_name]["id"], publish_type="Shader Mapping")
            asset_shader_mapping = sorted(asset_shader_mapping_data, key=lambda version: version["version_number"])[-1]
            asset_dict[asset_name]["shader_mapping"] = asset_shader_mapping["path"]['local_path_windows']
        else:
            asset_dict[asset_name]["items"].append(asset_ref_node)
    return asset_dict
